<template>
  <div>
     <div v-if="open">
     <voteOnline :voteId="voteId" :voteName="voteName" :cloneVoteOnline="closeVoteOnline"/>
     </div>
    <div v-if="!open" class="app-container">

      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="投票名称" prop="voteName">
          <el-input v-model="queryParams.voteName" placeholder="请输入投票名称" clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <!-- <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button> -->
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="投票名称" align="center" prop="voteName" />
        <el-table-column label="投票开始时间" align="center" prop="beginTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.beginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="投票结束时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.endTime<new Date()">已结束</el-tag>
            <el-tag type="success" v-else-if="scope.row.beginTime>new Date()">未开始</el-tag>
            <el-tag type="danger" v-else-if="scope.row.beginTime<new Date() && scope.row.endTime>new Date()">进行中</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="被投票人数" align="center" prop="importCount" />
        <el-table-column label="是否已投票" align="center" prop="isVote">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.isVote">已投票</el-tag>
            <el-tag type="success" v-else>未投票</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-if="!scope.row.isVote && scope.row.beginTime<new Date() && scope.row.endTime>new Date()" slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)"
                      v-hasPermi="['vote:online:query']">开始投票</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>

      <!-- 对话框(添加 / 修改) -->
      <!-- <el-dialog :title="title" :visible.sync="open" width="1000" append-to-body>
      
      </el-dialog> -->
    </div>
  </div>

</template>

<script>
import { getVotePage } from "@/api/vote/voteonline";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import VoteOnline from "./onlinevote.vue";
export default {
  name: "online",
  components: {
    VoteOnline,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投票规则列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否禁用voteType
      disabledVoteType: false,
      dateRangeBeginTime: [],
      dateRangeEndTime: [],
      dateRangeCreateTime: [],
      voteId: null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        voteName: null,
        voteCount: null,
        limitCount: null,
        remark: null,
        voteType: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      voteTypeDictDatas: getDictDatas(DICT_TYPE.VOTE_RULE_TYPE),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeBeginTime, 'beginTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getVotePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    closeVoteOnline() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        voteName: undefined,
        beginTime: undefined,
        endTime: undefined,
        voteCount: undefined,
        limitCount: undefined,
        remark: undefined,
        importCount: undefined,
      };
      this.disabledVoteType =false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "开始投票";
      this.voteId = row.id;
      this.voteName = row.voteName;
    },
    
   
  }
};
</script>
